/*
 * Created on 24/10/2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package gminet.infra.db;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * @author Luciana
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class TransactionToolkit {

    private TransactionToolkit() {
    }

    public static String getString(ResultSet rs, String fieldName) throws TransactionException {

        try {
            String value = rs.getString(fieldName);
            if (value == null) {
                value = "";
            }
            return value;
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static byte getByte(ResultSet rs, String fieldName) throws TransactionException {

        try {
            return rs.getByte(fieldName);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static int getInt(ResultSet rs, String fieldName) throws TransactionException {

        try {
            return rs.getInt(fieldName);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static float getFloat(ResultSet rs, String fieldName) throws TransactionException {

        try {
            return rs.getFloat(fieldName);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static Date getDate(ResultSet rs, String fieldName) throws TransactionException {

        try {
            return rs.getDate(fieldName);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    protected void setString(PreparedStatement statement, int pos, String value)
            throws TransactionException {

        try {
            if (value != null) {
                statement.setString(pos, value);
            } else {
                statement.setNull(pos, Types.VARCHAR);
            }
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static void setByte(PreparedStatement statement, int pos, byte value)
            throws TransactionException {

        try {
            statement.setByte(pos, value);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static void setInt(PreparedStatement statement, int pos, int value)
            throws TransactionException {

        try {
            statement.setInt(pos, value);
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static void setFloat(PreparedStatement statement, int pos, float value)
            throws TransactionException {

        try {
            if (!Float.isNaN(value) && !Float.isInfinite(value)) {
                statement.setFloat(pos, value);
            } else {
                statement.setNull(pos, Types.FLOAT);
            }
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

    public static void setDate(PreparedStatement statement, int pos, Date value)
            throws TransactionException {

        try {
            if (value != null) {
                statement.setDate(pos, value);
            } else {
                statement.setNull(pos, Types.DATE);
            }
        } catch (SQLException sqle) {
            throw new TransactionException(sqle);
        }
    }

}
